home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / IINIT.C < prev    next >
C/C++ Source or Header  |  1993-05-29  |  10KB  |  300 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* iinit.c */
  20. /* Initialize internally known objects for Ghostscript interpreter */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "dict.h"
  25. #include "dstack.h"
  26. #define INCLUDE_ERROR_NAMES        /* see errors.h */
  27. #include "errors.h"
  28. #include "ilevel.h"
  29. #include "iname.h"
  30. #include "oper.h"
  31. #include "save.h"            /* for alloc_refs */
  32. #include "store.h"
  33.  
  34. /* Define various parameters of this interpreter: */
  35. const char *gs_copyright =
  36.     "Copyright (C) 1990-1993 Aladdin Enterprises, Menlo Park, CA.\n";
  37. const char *gs_product =
  38.     "Ghostscript";
  39. const int gs_revision =
  40.     261;        /* release number x 100 + the sub-release. */
  41. const long gs_revisiondate =
  42.     930528;        /* year x 10000 + month x 100 + day. */
  43. const long gs_serialnumber =
  44.     42;        /* a well-known number */
  45.  
  46. /* Implementation parameters. */
  47. /* The sizes of systemdict can be set in the makefile. */
  48. /* We want the sizes to be prime numbers large enough to cover */
  49. /* all the operators, plus everything in the init files, */
  50. /* even if all the optional features are selected. */
  51. #ifndef SYSTEMDICT_SIZE
  52. #  define SYSTEMDICT_SIZE 479
  53. #endif
  54. #ifndef SYSTEMDICT_LEVEL2_SIZE
  55. #  define SYSTEMDICT_LEVEL2_SIZE 547
  56. #endif
  57. /* The size of level2dict, if applicable, can be set in the makefile. */
  58. #ifndef LEVEL2DICT_SIZE
  59. #  define LEVEL2DICT_SIZE 109
  60. #endif
  61. /* The number of permanent dstack entries can be set in the makefile. */
  62. #ifndef MIN_DSTACK_SIZE
  63. #  define MIN_DSTACK_SIZE 2
  64. #endif
  65. #define op_array_table_size 100        /* arbitrary */
  66.  
  67. /* The dictionary that holds Level 2 definitions when not installed. */
  68. ref ref_level2dict;
  69. #define level2dict (&ref_level2dict)
  70. /* Standard dictionaries */
  71. ref name_errordict;
  72. /* Error names */
  73. ref name_ErrorNames;
  74.  
  75. /* The operator tables */
  76. /* Because of a bug in Sun's SC1.0 compiler, */
  77. /* we have to spell out the typedef for op_def_ptr here: */
  78. const op_def _ds **op_def_table;
  79. uint op_def_count;
  80. ref op_array_table;    /* t_array, definitions of `operator' procedures */
  81. ushort *op_array_nx_table;        /* name indices for same */
  82. uint op_array_count;
  83.  
  84. /* Enter a name and value into systemdict */
  85. void
  86. initial_enter_string(const char *nstr, uint len, ref *pref)
  87. {    ref nref;
  88.     if ( name_ref((const byte *)nstr, len, &nref, 0) < 0 ||
  89.          dict_put(systemdict, &nref, pref) < 0
  90.        )
  91.         lprintf("initial_enter failed!\n"),
  92.         gs_exit(1);
  93. }
  94. void
  95. initial_enter_name(const char *nstr, ref *pref)
  96. {    initial_enter_string(nstr, strlen(nstr), pref);
  97. }
  98.  
  99. /* Initialize the operators. */
  100. /* Optional operators must come after standard ones, */
  101. /* so they can replace them. */
  102.     /* Non-graphics operators */
  103. extern op_def
  104.   zarith_op_defs[], zarray_op_defs[], zcontrol_op_defs[], zdict_op_defs[],
  105.   zfile_op_defs[], zfiledev_op_defs[], zfileio_op_defs[],
  106.   zfilter_op_defs[], zgeneric_op_defs[],
  107.   zmath_op_defs[], zmisc_op_defs[], zpacked_op_defs[], zprops_op_defs[],
  108.   zrelbit_op_defs[], zstack_op_defs[], zstring_op_defs[],
  109.   ztype_op_defs[], zvmem_op_defs[],
  110.     /* Graphics operators */
  111.   zchar_op_defs[], zcolor_op_defs[], zdevice_op_defs[],
  112.   zfont_op_defs[], zfont1_op_defs[], zfont2_op_defs[],
  113.   zgstate_op_defs[], zht_op_defs[],
  114.   zmatrix_op_defs[], zpaint_op_defs[], zpath_op_defs[],
  115.   zpath2_op_defs[],
  116.     /* Optional operators */
  117. #define oper_(defs) defs[],
  118. #define oper2_(defs) defs[],
  119. #include "gconfig.h"
  120. #undef oper_
  121. #undef oper2_
  122.     /* Interpreter operators */
  123.   interp_op_defs[];
  124. private op_def_ptr op_defs_all[] = {
  125.     /* Non-graphics operators */
  126.   zarith_op_defs, zarray_op_defs, zcontrol_op_defs, zdict_op_defs,
  127.   zfile_op_defs, zfiledev_op_defs, zfileio_op_defs,
  128.   zfilter_op_defs, zgeneric_op_defs,
  129.   zmath_op_defs, zmisc_op_defs, zpacked_op_defs, zprops_op_defs,
  130.   zrelbit_op_defs, zstack_op_defs, zstring_op_defs,
  131.   ztype_op_defs, zvmem_op_defs,
  132.     /* Graphics operators */
  133.   zchar_op_defs, zcolor_op_defs, zdevice_op_defs,
  134.   zfont_op_defs, zfont1_op_defs, zfont2_op_defs,
  135.   zgstate_op_defs, zht_op_defs,
  136.   zmatrix_op_defs, zpaint_op_defs, zpath_op_defs,
  137.   zpath2_op_defs,
  138.     /* Optional operators */
  139. #define oper_(defs) defs,
  140. #include "gconfig.h"
  141. #undef oper_
  142.     /* Interpreter operators */
  143.   interp_op_defs,
  144.     /* Optional Level 2 operators */
  145. #define oper2_(defs) defs,
  146. #include "gconfig.h"
  147. #undef oper2_
  148.     /* end marker */
  149.   (op_def_ptr)0
  150. };
  151. /* Detect whether we have any Level 2 operators. */
  152. #define have_level2\
  153.   (op_defs_all[countof(op_defs_all) - 2] != interp_op_defs)
  154.  
  155. /* Initialize the stacks (in interp.c) */
  156. extern void interp_init(P0());
  157.  
  158. /* Initialize objects other than operators */
  159. void
  160. obj_init(void)
  161. {
  162.     interp_init();
  163.  
  164.     /* Initialize the language level. */
  165.     make_int(&ref_language_level, 1);
  166.  
  167.     /* Create the built-in dictionaries. */
  168.     /* Only systemdict has non-zero maxlength. */
  169.     dict_create((have_level2 ? SYSTEMDICT_LEVEL2_SIZE : SYSTEMDICT_SIZE),
  170.             systemdict);
  171.     min_dstack_size = MIN_DSTACK_SIZE;
  172.     { int i;
  173.       for ( i = 1; i < MIN_DSTACK_SIZE; i++ )
  174.         dict_create(0, dsbot + i);
  175.     }
  176.  
  177.     /* Initialize the predefined names other than operators */
  178.     {    ref vtemp;
  179.         make_null(&vtemp);
  180.         initial_enter_name("null", &vtemp);
  181.         make_const_string(&vtemp, a_readonly, strlen(gs_copyright),
  182.                   (const byte *)gs_copyright);
  183.         initial_enter_name("copyright", &vtemp);
  184.         make_const_string(&vtemp, a_readonly, strlen(gs_product),
  185.                   (const byte *)gs_product);
  186.         initial_enter_name("product", &vtemp);
  187.         make_int(&vtemp, gs_revision);
  188.         initial_enter_name("revision", &vtemp);
  189.         make_int(&vtemp, gs_revisiondate);
  190.         initial_enter_name("revisiondate", &vtemp);
  191.         initial_enter_name("systemdict", systemdict);
  192.     }
  193.  
  194.     /* Create other system-known names */
  195.     name_enter("errordict", &name_errordict);
  196.     name_enter("ErrorNames", &name_ErrorNames);
  197.  
  198.     /* Create the error name table */
  199.        {    int n = sizeof(gs_error_names) / sizeof(char _ds *) - 1;
  200.         int i;
  201.         ref era;
  202.         alloc_array(&era, a_readonly, n, "obj_init(ErrorNames)");
  203.         for ( i = 0; i < n; i++ )
  204.           name_enter((char *)gs_error_names[i], era.value.refs + i);
  205.         dict_put(systemdict, &name_ErrorNames, &era);
  206.        }
  207. }
  208.  
  209. /* Run the initialization procedures of the individual operator files. */
  210. void
  211. zop_init(void)
  212. {    op_def_ptr _ds *tptr;
  213.     /* Because of a bug in Sun's SC1.0 compiler, */
  214.     /* we have to spell out the typedef for op_def_ptr here: */
  215.     const op_def _ds *def;
  216.     for ( tptr = op_defs_all; *tptr != 0; tptr++ )
  217.        {    for ( def = *tptr; def->oname != 0; def++ ) ;
  218.         if ( def->proc != 0 )
  219.             ((void (*)(P0()))(def->proc))();
  220.        }
  221. }
  222. /* Initialize the operator table. */
  223. void
  224. op_init(void)
  225. {    int count = 1;
  226.     int interp_count;
  227.     op_def_ptr _ds *tptr;
  228.     /* Because of a bug in Sun's SC1.0 compiler, */
  229.     /* we have to spell out the typedef for op_def_ptr here: */
  230.     const op_def _ds *def;
  231.     const char _ds *nstr;
  232.  
  233.     /* Do a first pass just to count the operators. */
  234.     /* Note the value of count after interp_op_defs, because */
  235.     /* that is where the Level 2 operators start. */
  236.  
  237.     for ( tptr = op_defs_all; *tptr != 0; tptr ++ )
  238.     {    for ( def = *tptr; def->oname != 0; count++, def++ )
  239.             ;
  240.         if ( *tptr == interp_op_defs )
  241.             interp_count = count;
  242.     }
  243.     
  244.     if ( interp_count != count )
  245.     {    /* Create level2dict for the Level 2 definitions. */
  246.         dict_create(LEVEL2DICT_SIZE, level2dict);
  247.         initial_enter_name("level2dict", level2dict);
  248.     }
  249.  
  250.     /* Do a second pass to construct the operator table, */
  251.     /* and enter the operators in systemdict or level2dict. */
  252.  
  253.     /* Because of a bug in Sun's SC1.0 compiler, */
  254.     /* we have to spell out the typedef for op_def_ptr here: */
  255.     op_def_table =
  256.         (const op_def _ds **)alloc(count, sizeof(op_def_ptr),
  257.                        "op_init(op_def_table)");
  258.     op_def_count = count;
  259.     count = 1;
  260.     for ( tptr = op_defs_all; *tptr != 0; tptr ++ )
  261.      for ( def = *tptr; (nstr = def->oname) != 0; count++, def++ )
  262.        {    ref nref, oper;
  263.         make_oper(&oper, count, (dummy_op_proc_p)(def->proc));
  264.         interp_fix_op(&oper);        /* optimize if possible */
  265.         /* The first character of the name is a digit */
  266.         /* giving the minimum acceptable number of operands. */
  267.         /* For now, we just skip over it. */
  268.         nstr++;
  269.         /* Don't enter internal operators into systemdict. */
  270.         if ( *nstr == '%' )
  271.             name_enter(nstr, &nref);
  272.         else
  273.         {    ref nref;
  274.             ref *pdict =
  275.               (count >= interp_count ? level2dict : systemdict);
  276.             name_enter(nstr, &nref);
  277.             dict_put(pdict, &nref, &oper);
  278.         }
  279.         op_def_table[count] = def;
  280.        }
  281.  
  282.     /* Allocate the table for `operator' procedures. */
  283.  
  284.     alloc_array(&op_array_table, a_readonly, op_array_table_size,
  285.             "op_array table");
  286.     refset_null(op_array_table.value.refs, op_array_table_size);
  287.     op_array_nx_table =
  288.       (ushort *)alloc(op_array_table_size, sizeof(ushort),
  289.               "op_array nx table");
  290.     op_array_count = 0;
  291.  
  292. }
  293.  
  294. /* Initialize variables that hold name constants. */
  295. void
  296. init_names(register const names_def _ds *pnd)
  297. {    for ( ; pnd->vname != 0; pnd++ )
  298.         name_enter(pnd->vname, pnd->pvref);
  299. }
  300.